package com.gitee.wsl.jvm.reflect.autobox

import java.time.Duration

class DurationTypeAutoboxer : TypeAutoboxer<Duration> {
    override fun supports(type: Class<*>): Boolean {
        return Duration::class.java == type
    }

    override fun autobox(value: Any, type: Class<Duration>): Duration {
        if (value is Duration) {
            return value
        } else if (value is CharSequence) {
            return Duration.parse(value)
        }
        throw UnsupportedOperationException(
            String.format(
                "Cannot autobox %s of type %s to %s",
                value,
                value.javaClass.name,
                Duration::class.java.name
            )
        )
    }
}
